<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//This is disabled!
exit('No direct script access allowed');

class Import extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		
		
	}

	function index()
	{
		//$time_limit = 10*60*60; //ten hours
	
		ini_set('display_errors',1);
		error_reporting(E_ALL);

	
		$this->db->save_queries = FALSE;
		set_time_limit (120);
		
		$query = $this->db->select('count(*) as c')
		         ->from('geo_location')
				 ->get();
				 
		$res = $query->result();
		$start_point = $res[0]->c;
		
		echo 'start = '.$start_point;
		
		try
		{
			$import_file = fopen("/home/zwarefi/_allCountries.txt",'r');
			$date_starting = new DateTime();
			
			echo 'Started at: ' . $date_starting->format('Y-m-d H:i:s').'<br/>';
			if($import_file)
			{
				$count = 0;
				$max = 0;
				$step = 5000;
				$s = fgets($import_file, 20000);
				while($s)
				{
					$count++;
					if($count > $start_point)
					{
						if($max>0 && $count > $max)
						{
							echo 'completed ' . $count .' lines and exiting <br/>';
							break;
						}
						
						//echo $s;
						
						$data = explode("\t",$s);
						
						if(count($data) != 19)
						{
							echo 'weird row: '; print_r($data); echo '<br/>';
							set_time_limit ( 120 );
						}
						else
						{
							$this->db->set('geonameid', $data[0])
								 ->set('name', $data[1])
								 ->set('ascii_name', $data[2])
								 ->set('alternate_names', $data[3])
								 ->set('latitude', $data[4])
								 ->set('longitude', $data[5])
								 ->set('feature_class', $data[6])
								 ->set('feature_code', $data[7])
								 ->set('country_code', $data[8])
								 ->set('country_code2', $data[9])
								 ->set('region_code1', $data[10])
								 ->set('region_code2', $data[11])
								 ->set('region_code3', $data[12])
								 ->set('region_code4', $data[13])
								 ->set('population', $data[14])
								 ->set('elevation', $data[15])
								 ->set('gtopo30', $data[16])
								 ->set('timezone', $data[17])
								 ->set('modification_date', $data[18])
								 ->insert('geo_location');
						}
						
						if($count % $step == 0)
						{
							$pass_time = new DateTime();
							echo 'Count: '.$count.' at time: '.$pass_time->format('H:i:s').' <br/>';
							set_time_limit ( 120 );
						}
					}
					else
					{
						if($count % $step == 0)
						{
							$pass_time = new DateTime();
							echo 'Skipping: '.$count.' at time: '.$pass_time->format('H:i:s').' <br/>';
							set_time_limit ( 120 );
						}
					}
					$s = fgets($import_file, 20000);
					if(!$s)
					{
						echo 'Did not read the line from the file, will exit. Count = ' . $count . '<br/>';
					}
				}
				fclose($import_file);
			}
		} catch (Exception $e) {
			echo 'Caught exception: ',  $e->getMessage(), ' Count = ' . $count . '<br/>';
		}
		
		echo 'Exited the loop. Count = ' . $count . '<br/>';
	}
}
